package com.crm.service.impl.sys.permissionManagement;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.permissionManagement.SysMenuPermissionCustomMapper;
import com.crm.dao.mapper.sys.permissionManagement.SysMenuPermissionMapper;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysMenuPermission;
import com.crm.model.entity.sys.permissionManagement.SysMenuPermissionExample;
import com.crm.service.sys.permissionManagement.SysMenuPermissionDbService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author yincuimei
 * @Date 2020/12/6 19:31
 */
@Service
public class SysMenuPermissionDbServiceImpl implements SysMenuPermissionDbService {
    @Autowired
    private SysMenuPermissionMapper sysMenuPermissionMapper;

    @Autowired
    SysMenuPermissionCustomMapper sysMenuPermissionCustomMapper;

    /**
     * 新增一条菜单-权限组关联数据
     * @param sysMenuPermission ---(菜单-权限组信息)
     * @return
     */
    @Override
    public Boolean insertSysMenuPermission(SysMenuPermission sysMenuPermission) {
        return sysMenuPermissionMapper.insert(sysMenuPermission) > 0;
    }

    /**
     * 批量添加菜单-权限组关联数据
     * @param sysMenuPermissionList
     * @return
     */
    @Override
    public Boolean insertSysMenuPermissionBatch(List<SysMenuPermission> sysMenuPermissionList) {
        return sysMenuPermissionCustomMapper.insertSysMenuPermissionBatch(sysMenuPermissionList) > 0;
    }

    @Override
    public Boolean deleteSysMenuPermissionByPGId(String permissionGroupId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        SysMenuPermissionExample sysMenuPermissionExample = new SysMenuPermissionExample();
        SysMenuPermissionExample.Criteria criteria = sysMenuPermissionExample.createCriteria();
        if (StringUtils.isNotEmpty(permissionGroupId)) {
            criteria.andPermissionGroupIdEqualTo(permissionGroupId);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        }
        SysMenuPermission sysMenuPermission = new SysMenuPermission();
        sysMenuPermission.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        sysMenuPermission.setUpdatePeopleId(userAccount.getAccountId());
        sysMenuPermission.setUpdateTm(new Date());
        return sysMenuPermissionMapper.updateByExampleSelective(sysMenuPermission, sysMenuPermissionExample) > 0;
    }

    @Override
    public List<String> selectMenuPermissionIdList(String permissionGroupId) {
        return sysMenuPermissionCustomMapper.selectMenuPermissionIdList(permissionGroupId);
    }

    @Override
    public List<SysMenuPermission> selectSysMenuPermissionList(String permissionGroupId) {
        SysMenuPermissionExample sysMenuPermissionExample = new SysMenuPermissionExample();
        SysMenuPermissionExample.Criteria criteria = sysMenuPermissionExample.createCriteria();
        if (StringUtils.isNotEmpty(permissionGroupId)) {
            criteria.andPermissionGroupIdEqualTo(permissionGroupId);
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        }
        return sysMenuPermissionMapper.selectByExample(sysMenuPermissionExample);
    }
}
